<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>register</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css">
    <!--bootstrap .bundle.minjs用于弹窗 、提示、下拉菜单，包含了popper.min.js-->
    <script src="https:/cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js"></script>
    <!--最新的Bootstrap4核心JavaScript文件-->
    <script src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
    <script type="text/javascript">
        function isNull(str) {
            if (str == "")
                return true;
            var regu = "^[ ]+$";
            var re = new RegExp(regu);
            return re.test(str);
        }
        $(function () {
            $("#div4addUser").hide();
            $("#btn4showhide").click(function () {
                if ($("#div4addUser").is(":hidden"))
                    $("#div4addUser").show();
                else
                    $("#div4addUser").hide();
            });
            $("#add").click(function () {
                var nameVal = $("#name").val();
                var pwdVal = $("#pwd").val();
                var roleVal = $("#role option:selected").text();
                if (isNull(nameVal) || isNull(pwdVal) || isNull(roleVal)) {
                    confirm("请完善信息");
                    console.log("点击了");
                    return;
                }
                $("#add").submit();
                $("#addUser").action="/addUser";
                $("#addUser").enctype="multipart/form-data";
                $("#addUser").method="post";
                $("#addUser").submit();

                var param = {
                    "name":nameVal,
                    "password":pwdVal,
                    "role":roleVal
                };
                $.ajax({
                    url:"/addUser",
                    data:param,
                    async:true,
                    success:function f(result){
                        window.confirm(result.msg);
                        location.href = "users";
                    }
                });
            });
            $("#addFile").click(function () {
                $("#fileSpan").append("<input type = 'file' name = 'file'><a id = 'delFile' class = 'delFile' style='cursor:pointer;color:red'>删除该文件</a><br>");
                $(".delFile").click(function () {
                    $($(this).next()).remove();
                    $($(this).prev()).remove();
                    $(this).remove();
                });
            });
            $("#delFile").click(function () {
                $($(this).next()).remove();
                $($(this).prev()).remove();
                $(this).remove();
            });
            $(".fileSpan").on("click", ".delFile", function () {
                $($(this).next()).remove();
                $($(this).prev()).remove();
                $(this).remove();
            });
        });
    </script>
</head>
<body>
<div id="addUser">
    <label style="display: block;margin: 0 auto">添加用户</label>
    <form name="addUser" action="/addUser" method="post" enctype="multipart/form-data">
        <div class="form-group">
            <label for="name">name:</label>
            <input type="text" class="form-control" name="name" id="name">
        </div>
        <div class="form-group">
            <label for="password">Password:</label>
            <input type="password" class="form-control" name="password" id="password">
        </div>
        <div class="form-group">
            <label for="role">role:</label>
            <select class="form-control" name="role" id="role">
                <option>admin</option>
                <option>user</option>
            </select>
        </div>
        <div class="form-group">
                <span id="fileSpan" class="fileSpan">
                    <input type="file" name="file">
                    <a id="delFile" class="delFile" style="cursor:pointer;color:red">删除该文件</a><br>
                </span>
            <a id="addFile" style="cursor:pointer;color: red">添加新文件</a>
        </div>
        <input id="add" type="submit" value="添加用户" />
    </form>
    <!--    <button id="add">AddUser</button>-->
</div>
</body>
</html>